/**
 * @Author: 17197
 * @Date: 2024-05-01 14:31:32
 * @Description: articleController.js
 * @Version: 1.0
 * @Last Modified time : 2024-05-01 14:31:32
 **/
const articleModule = require('../../models/article/articleModel');
const jwt = require("jsonwebtoken");

// 获取文章列表
exports.getArticleList = async (req, res) => {
	try {
		const {Page, Limit, Title, Status, Admin, Sort} = req.query;
		const token = req.signedCookies.access_token;
		if (token) {
			jwt.verify(token, process.env.TOKEN_SECRET, (err, decoded) => {
				if (err) {
					return
				}
				req.user = decoded; // 将解析出来的用户信息赋值给req.user
			});
		}
		const result = await articleModule.getArticleList(Page, Limit, Title, Status, Admin, Sort);
		if (!result) {
			console.log('获取文章列表失败');
			return res.sendRes(null, 400, '获取文章列表失败');
		}
		// isLike, isCollect,true:已点赞/已收藏，false:未点赞/未收藏
		for (const item of result.Rows) {
			item.avatar = process.env.BASE_URL + item.avatar;
			if (token && req.user) {
				item.isLike = await articleModule.queryArticleIsLike(item.id, req.user.id)
				item.isCollect = await articleModule.queryArticleIsCollect(item.id, req.user.id)
			}
		}
		console.log('获取文章列表成功');
		return res.sendRes(result, 200, 'success');
	} catch (error) {
		console.error(error);
		return res.sendRes(null, 500, '服务器错误');
	}
}

// 获取文章详情
exports.getArticleDetail = async (req, res) => {
	try {
		const {Id} = req.query;
		const token = req.signedCookies.access_token;
		const result = await articleModule.getArticleDetail(Id);
		if (!result) {
			console.log('获取文章详情失败');
			return res.sendRes(null, 400, '获取文章详情失败');
		}
		if (token) {
			jwt.verify(token, process.env.TOKEN_SECRET, (err, decoded) => {
				if (err) {
					return
				}
				req.user = decoded; // 将解析出来的用户信息赋值给req.user
			});
		}
		console.log('获取文章详情成功');
		result.avatar = process.env.BASE_URL + result.avatar;
		if (token && req.user) {
			result.isLike = await articleModule.queryArticleIsLike(result.id, req.user.id)
			result.isCollect = await articleModule.queryArticleIsCollect(result.id, req.user.id)
		}
		return res.sendRes(result, 200, 'success');
	} catch (error) {
		console.error(error);
		return res.sendRes(null, 500, '服务器错误');
	}
}

// 发布文章
exports.publishArticle = async (req, res) => {
	try {
		const data = req.body;
		const result = await articleModule.publishArticle(data);
		if (!result) {
			console.log('发布文章失败');
			return res.sendRes(null, 400, '发布文章失败');
		}
		console.log('发布文章成功');
		return res.sendRes(null, 200, 'success');
	} catch (error) {
		console.error(error);
		return res.sendRes(null, 500, '服务器错误');
	}
}

// 删除文章
exports.deleteArticle = async (req, res) => {
	try {
		const {Id, UserID} = req.query;
		const result = await articleModule.deleteArticle(Id);
		if (!result) {
			console.log('删除文章失败');
			return res.sendRes(null, 400, '删除文章失败');
		}
		console.log('删除文章成功');
		return res.sendRes(null, 200, 'success');
	} catch (error) {
		console.error(error);
		return res.sendRes(null, 500, '服务器错误');
	}
}

// 获取必读榜
exports.getReadList = async (req, res) => {
	try {
		const result = await articleModule.getReadList();
		if (!result) {
			console.log('获取必读榜失败');
			return res.sendRes(null, 400, '获取必读榜失败');
		}
		result.forEach(item => {
			item.avatar = process.env.BASE_URL + item.avatar;
		})
		console.log('获取必读榜成功');
		return res.sendRes(result, 200, 'success');
	} catch (error) {
		console.error(error);
		return res.sendRes(null, 500, '服务器错误');
	}
}

// 获取文章评论
exports.getArticleComment = async (req, res) => {
	try {
		// 分类和排序（1：最新，2：最热）
		const {ArticleID, Page, Limit, Sort} = req.query;
		const result = await articleModule.getArticleComment(ArticleID, Page, Limit, Sort);
		if (!result) {
			console.log('获取文章评论失败');
			return res.sendRes(null, 400, '获取文章评论失败');
		}
		// 点赞和子评论处理
		const token = req.signedCookies.access_token;
		if (token) {
			jwt.verify(token, process.env.TOKEN_SECRET, (err, decoded) => {
				if (err) {
					return
				}
				req.user = decoded; // 将解析出来的用户信息赋值给req.user
			});
		}
		const subComment = [];
		const toLevelComment = [];
		for (const item of result.rows) {
			item.avatar = process.env.BASE_URL + item.avatar;
			if (token && req.user && item.like_accounts > 0) {
				item.isLike = await articleModule.queryIsLike(item.id, req.user.id)
			}
			if (item.parent_comment_id) {
				subComment.push(item);
			} else {
				toLevelComment.push(item);
			}
		}
		toLevelComment.forEach(item => {
			item.subComment = subComment.filter(subItem => subItem.parent_comment_id === item.id);
		})
		console.log('获取文章评论成功');
		return res.sendRes({
			Total: result.total,
			Rows: toLevelComment
		}, 200, 'success');
	} catch (error) {
		console.error(error);
		return res.sendRes(null, 500, '服务器错误');
	}
}

// 评论点赞
exports.commentLike = async (req, res) => {
	try {
		const {CommentID} = req.body;
		const result = await articleModule.commentLike(CommentID, req.user.id);
		if (!result) {
			console.log('评论点赞失败');
			return res.sendRes(null, 400, '评论点赞失败');
		}
		console.log('评论点赞成功');
		return res.sendRes(null, 200, 'success');
	} catch (error) {
		console.error(error);
		return res.sendRes(null, 500, '服务器错误');
	}
}

// 发表评论
exports.publishComment = async (req, res) => {
	try {
		const data = req.body;
		const result = await articleModule.publishComment(data, req.user.id);
		if (!result) {
			console.log('发表评论失败');
			return res.sendRes(null, 400, '发表评论失败');
		}
		console.log('发表评论成功');
		return res.sendRes(null, 200, 'success');
	} catch (error) {
		console.error(error);
		return res.sendRes(null, 500, '服务器错误');
	}
}

// 删除评论
exports.deleteComment = async (req, res) => {
	try {
		const {CommentID} = req.query;
		const result = await articleModule.deleteComment(CommentID, req.user.id);
		if (!result) {
			console.log('删除评论失败');
			return res.sendRes(null, 400, '删除评论失败');
		}
		console.log('删除评论成功');
		return res.sendRes(null, 200, 'success');
	} catch (error) {
		console.error(error);
		return res.sendRes(null, 500, '服务器错误');
	}
}

// 文章点赞
exports.articleLike = async (req, res) => {
	try {
		const {ArticleID} = req.query;
		const result = await articleModule.articleLike(ArticleID, req.user.id);
		if (!result) {
			console.log('文章点赞失败');
			return res.sendRes(null, 400, '文章点赞失败');
		}
		console.log('文章点赞成功');
		return res.sendRes(null, 200, 'success');
	} catch (error) {
		console.error(error);
		return res.sendRes(null, 500, '服务器错误');
	}
}

// 文章收藏
exports.articleCollect = async (req, res) => {
	try {
		const {ArticleID} = req.query;
		const result = await articleModule.articleCollect(ArticleID, req.user.id);
		if (!result) {
			console.log('文章收藏失败');
			return res.sendRes(null, 400, '文章收藏失败');
		}
		console.log('文章收藏成功');
		return res.sendRes(null, 200, 'success');
	} catch (error) {
		console.error(error);
		return res.sendRes(null, 500, '服务器错误');
	}
}

